package cn.edu.cdp.dumpling.controller;

import cn.edu.cdp.dumpling.entity.Category;
import cn.edu.cdp.dumpling.service.CategoryService;
import cn.edu.cdp.dumpling.util.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 菜品类别管理
@RestController
//@CrossOrigin
@RequestMapping("/categories")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    // 查看
    @GetMapping
    public R listAll() {
        List<Category> list = categoryService.list();
        if (list == null || list.isEmpty())
            return R.error("查询失败");
        else
            return R.success("查询成功", list);
    }

    // 增加
    @PostMapping
    public R save(@RequestBody Category category) {
        if (categoryService.save(category))
            return R.success("添加成功", null);
        else
            return R.error("添加失败");
    }

    // 删除
    @DeleteMapping("{id}")
    public R removeById(@PathVariable Integer id) {
        if (categoryService.removeById(id))
            return R.success("删除成功", null);
        else
            return R.error("删除失败");
    }

    // 修改菜品类别
    @PutMapping
    public R updateById(@RequestBody Category category) {
        if (categoryService.updateById(category))
            return R.success("修改成功", null);
        else
            return R.error("修改失败");
    }
}
